//
//  CarpoolingListCell.m
//  cityStar
//
//  Created by joyban on 15-1-20.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "CarpoolingListCell.h"
#import "UIColor+FlatUI.h"

@implementation CarpoolingListCell

+(CGFloat)heightOfTableCell
{
    return 88;
}


- (void)awakeFromNib {
    // Initialization code
}

-(void)setCityPollingData:(CarpoolingListModel *)model
{
    self.titleLabel.text = model.title;
    NSString *str = @"";
    NSArray *discountArr = [[NSString stringWithFormat:@"%@",model.way] componentsSeparatedByString:@" "];
    if(discountArr.count > 1)
    {
        NSString *string = discountArr[0];
        for (int i = 1; i<discountArr.count; i++) {
            string = [NSString stringWithFormat:@"%@-%@",string,discountArr[i]];
        }
        str = string;
    }else{
        str = discountArr[0];
    }
    
    NSString *typestrinf = [NSString stringWithFormat:@"%@",model.carpool_type];
    
    if([typestrinf isEqualToString:@"0"])
    {
        self.leftLine.constant = 250;
        self.typeLabel.text = @"上下班拼车";
    }else if([typestrinf isEqualToString:@"1"])
    {
        self.leftLine.constant = 240;
        self.typeLabel.text = @"单程旅行拼车";
    }else{
        self.leftLine.constant = 220;
        self.typeLabel.text = @"往返双程旅行拼车";
    }
    
    NSInteger typestring = [[NSString stringWithFormat:@"%@",model.car_model] integerValue];
    switch (typestring) {
        case 0:
            self.carTypeLabel.text = @"经济型";
            break;
        case 1:
            self.carTypeLabel.text = @"舒适型";
            break;
        case 2:
            self.carTypeLabel.text = @"豪华型";
            break;
        default:
            break;
    }
    
    self.tujingLabel.text =[NSString stringWithFormat:@"%@-%@-%@",model.start,str,model.end];
    self.telphoneLabel.text = @"";
    NSDate *fastTime = [NSDate dateWithTimeIntervalSince1970:[[NSString stringWithFormat:@"%@",model.departure] intValue]];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MM月dd日 HH:mm出发"];
    NSString *  locationString=[dateformatter stringFromDate:fastTime];
    self.NameLabel.text = locationString;
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[NSString stringWithFormat:@"%@",model.modify_time] intValue]];
    self.timeLabel.text = [self dateStringFromNow:confromTimesp];
    NSString *status = [NSString stringWithFormat:@"%@",model.status];
    if([status isEqualToString:@"0"])
    {
        self.titleLabel.textColor = [UIColor colorFromHexCode:@"949494"];
        NSString *name = [NSString stringWithFormat:@"%@%@",@"[已作废]",model.title];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:name];
        [attri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
        [self.titleLabel setAttributedText:attri];
        self.NameLabel.textColor = [UIColor colorFromHexCode:@"949494"];
        self.peoplelabel.textColor = [UIColor colorFromHexCode:@"949494"];
        self.typeLabel.backgroundColor = [UIColor colorFromHexCode:@"949494"];
//        self.carTypeLabel.backgroundColor = [UIColor colorFromHexCode:@"949494"];
    }else{
        self.titleLabel.textColor = [UIColor colorFromHexCode:@"333333"];
        self.NameLabel.textColor = [UIColor colorFromHexCode:@"ff9b5f"];
        self.peoplelabel.textColor = [UIColor colorFromHexCode:@"ff9b5f"];
        self.typeLabel.backgroundColor = [UIColor colorFromHexCode:@"ff7676"];
//        self.carTypeLabel.backgroundColor = [UIColor colorFromHexCode:@"ff9b5f"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(NSString*)dateStringFromNow:(NSDate*)date{
    NSTimeInterval time = -[date timeIntervalSinceNow];
    if (time<0) {
        return @"";
    }else {
        if (time<3600*24) {
            if (time>=0.0 && time <60.0) {
                return @"刚刚";
            }else if (time>=60.0 && time <60.0*60.0) {
                return [NSString stringWithFormat:@"%d分钟前",(int)time/60];
            }else if (time>=3600.0 && time <3600.0*24) {
                return [NSString stringWithFormat:@"%d小时前",(int)time/3600];;
            }
        }else {
            
            NSCalendar* calendar = [NSCalendar currentCalendar];
            NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit fromDate:date toDate:[NSDate date] options:0];
            if ([components year]>0) {
                int month = [components year]*12+[components month];
                return [NSString stringWithFormat:@"%d月前",month];
            }else if([components month]>0){
                int month = [components month];
                return [NSString stringWithFormat:@"%d月前",month];
            }else if([components week]>0){
                return [NSString stringWithFormat:@"%d周前",[components week]];
            }else if([components day]>0){
                return [NSString stringWithFormat:@"%d天前", [components day]];
            }
        }
    }
    return @"";
}

@end
